public class 命令模式 {
    public static void main(String[] args) {
        // 创建接收者对象
        LightA light = new LightA();
        Fan fan = new Fan();
        // 创建命令对象并绑定接收者
        Command lightOn = new LightOnCommand(light);
        Command lightOff = new LightOffCommand(light);
        Command fanStart = new FanStartCommand(fan);
        Command fanStop = new FanStopCommand(fan);

        // 创建遥控器对象
        RemoteControl remote = new RemoteControl();

        // 设置并执行命令
        remote.setCommand(lightOn);
        remote.pressButton();  // 开灯

        remote.setCommand(fanStart);
        remote.pressButton();  // 开风扇

        remote.setCommand(lightOff);
        remote.pressButton();  // 关灯

        remote.setCommand(fanStop);
        remote.pressButton();  // 关风扇



    }
}
//1. 定义命令接口
interface Command {
    void execute();
}
///2. 定义接收者（电灯、风扇）
// 电灯（Receiver）
 class LightA {
    public void on() {
        System.out.println("The light is ON");
    }

    public void off() {
        System.out.println("The light is OFF");
    }
}

// 风扇（Receiver）
 class Fan {
    public void start() {
        System.out.println("The fan is ON");
    }

    public void stop() {
        System.out.println("The fan is OFF");
    }
}
//3. 定义具体命令类 我们定义具体的命令类，封装具体的请求（比如开灯、关风扇）。

// 开灯命令（具体命令）
 class LightOnCommand implements Command {
    private LightA light;

    public LightOnCommand(LightA light) {
        this.light = light;
    }

    @Override
    public void execute() {
        light.on();
    }
}
// 关灯命令（具体命令）
 class LightOffCommand implements Command {
    private LightA light;

    public LightOffCommand(LightA light) {
        this.light = light;
    }

    @Override
    public void execute() {
        light.off();
    }
}
// 启动风扇命令（具体命令）
 class FanStartCommand implements Command {
    private Fan fan;

    public FanStartCommand(Fan fan) {
        this.fan = fan;
    }

    @Override
    public void execute() {
        fan.start();
    }
}
// 停止风扇命令（具体命令）
 class FanStopCommand implements Command {
    private Fan fan;

    public FanStopCommand(Fan fan) {
        this.fan = fan;
    }

    @Override
    public void execute() {
        fan.stop();
    }
}
 //遥控器（调用者）
  class RemoteControl {
     private Command command;

     public void setCommand(Command command) {
         this.command = command;
     }

     public void pressButton() {
         command.execute();
     }
 }